package com.rentong.cust.service.impl;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.github.pagehelper.util.StringUtil;
import com.rentong.common.utils.DateUtils;
import com.rentong.cust.vo.CustMaintenanceDto;
import com.rentong.cust.vo.ExportExcel;
import com.rentong.cust.vo.ExportExcelDto;
import com.rentong.cust.vo.MaintenanceVo;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rentong.cust.mapper.CustMaintenanceMapper;
import com.rentong.cust.domain.CustMaintenance;
import com.rentong.cust.service.ICustMaintenanceService;
import org.springframework.util.CollectionUtils;

/**
 * 维修表列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-30
 */
@Service
public class CustMaintenanceServiceImpl implements ICustMaintenanceService {
    @Autowired
    private CustMaintenanceMapper custMaintenanceMapper;

    /**
     * 查询维修表列表
     *
     * @param maintenanceId 维修表列表主键
     * @return 维修表列表
     */
    @Override
    public MaintenanceVo selectCustMaintenanceByMaintenanceId(Long maintenanceId) {
        return custMaintenanceMapper.selectCustMaintenanceByMaintenanceId(maintenanceId);
    }


    @Override
    public List<MaintenanceVo> selectCustMaintenanceList(CustMaintenanceDto custMaintenanceDto) {

        return custMaintenanceMapper.selectCustMaintenanceList(custMaintenanceDto);
    }

    /**
     * 新增维修表列表
     *
     * @param custMaintenance 维修表列表
     * @return 结果
     */
    @Override
    public int insertCustMaintenance(CustMaintenance custMaintenance) {
        return custMaintenanceMapper.insertCustMaintenance(custMaintenance);
    }

    /**
     * 修改维修表列表
     *
     * @param custMaintenance 维修表列表
     * @return 结果
     */
    @Override
    public int updateCustMaintenance(CustMaintenance custMaintenance) {
        custMaintenance.setUpdateTime(DateUtils.getNowDate());
        return custMaintenanceMapper.updateCustMaintenance(custMaintenance);
    }

    /**
     * 批量删除维修表列表
     *
     * @param maintenanceIds 需要删除的维修表列表主键
     * @return 结果
     */
    @Override
    public int deleteCustMaintenanceByMaintenanceIds(Long[] maintenanceIds) {
        return custMaintenanceMapper.deleteCustMaintenanceByMaintenanceIds(maintenanceIds);
    }

    /**
     * 删除维修表列表信息
     *
     * @param maintenanceId 维修表列表主键
     * @return 结果
     */
    @Override
    public int deleteCustMaintenanceByMaintenanceId(Long maintenanceId) {
        return custMaintenanceMapper.deleteCustMaintenanceByMaintenanceId(maintenanceId);
    }

    public void exportUsingTemplate(String maintenanceIds) throws IOException {
        if (!StringUtil.isEmpty(maintenanceIds)) {
            String[] split = maintenanceIds.split(",");
            List<ExportExcelDto> exportExcelDtos = new ArrayList<>();
            for (String s : split) {
                exportExcelDtos.add(custMaintenanceMapper.export(Integer.parseInt(s)));
            }
            List<ExportExcel> list = new ArrayList<>();
            for (ExportExcelDto exportExcelDto : exportExcelDtos) {
                ExportExcel exportExcel = new ExportExcel();
                exportExcel.setEventDescription(exportExcelDto.getEventDescription());
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日");
                exportExcel.setMaintenanceTime(exportExcelDto.getMaintenanceTime().format(formatter));
                exportExcel.setV3("");
                exportExcel.setV4("");
                exportExcel.setV5("");
                exportExcel.setV6("");
                list.add(exportExcel);
                if (!CollectionUtils.isEmpty(exportExcelDto.getCustMaintenanceListName())) {
                    for (String name : exportExcelDto.getCustMaintenanceListName()) {
                        ExportExcel exportExcel2 = new ExportExcel();
                        exportExcel2.setMaintenanceTime("");
                        exportExcel2.setEventDescription(name);
                        exportExcel2.setV3("");
                        exportExcel2.setV4("");
                        exportExcel2.setV5("");
                        exportExcel2.setV6("");
                        list.add(exportExcel2);
                    }
                }
            }
            salaryList(list);
        }

    }

    public void salaryList(List<ExportExcel> list) {
        // 模板文件路径
        String templateFilePath = "D:\\2.xlsx";
        // 输出文件路径
        String outFilePath = "D:\\gzb.xlsx";

        // 创建 ExcelWriter 实例
        ExcelWriter writer = EasyExcel
                // 写入到
                .write(outFilePath)
                // 指定模板
                .withTemplate(templateFilePath)
                .build();

        WriteSheet sheet = EasyExcel.writerSheet().build();


        FillConfig fillConfig = FillConfig.builder()
                // 开启填充换行
                .forceNewRow(true)
                .build();

        // 执行填充操作
        writer.fill(list, fillConfig, sheet);

        // 结束
        writer.finish();
    }

    private List<List<String>> head() {
        List<List<String>> list = ListUtils.newArrayList();
        List<String> head0 = ListUtils.newArrayList();
        head0.add("序号");
        List<String> head1 = ListUtils.newArrayList();
        head1.add("工程名称");
        list.add(head0);
        list.add(head1);
        return list;
    }

    private List<List<Object>> dataList(List<ExportExcelDto> exportExcelDtos) {
        List<List<Object>> list = ListUtils.newArrayList();
        for (ExportExcelDto exportExcelDto : exportExcelDtos) {
            List<Object> data = ListUtils.newArrayList();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日");
            data.add(exportExcelDto.getMaintenanceTime().format(formatter));
            data.add(exportExcelDto.getEventDescription());
            list.add(data);
            if (!CollectionUtils.isEmpty(exportExcelDto.getCustMaintenanceListName())) {
                for (String name : exportExcelDto.getCustMaintenanceListName()) {
                    List<Object> data1 = ListUtils.newArrayList();
                    data1.add("");
                    data1.add(name);
                    list.add(data1);
                }
            }
        }
        return list;
    }

    public void noModelWrite(List<ExportExcelDto> exportExcelDtos) {
        // 设置列宽自动调整
//        WriteCellStyle headerStyle = new WriteCellStyle();
//        headerStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//
//        // 设置行高（单位是像素）
//        int rowHeight = 20; // 设置行高为20像素
        // 写法1
        String fileName = "D:\\ExportedData9.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName).registerWriteHandler(this.getHorizontalCellStyleStrategy((short) 12)).registerWriteHandler(new SimpleColumnWidthStyleStrategy(39)).
                head(head()).sheet("模板").doWrite(dataList(exportExcelDtos));
    }

    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy(Short fontHeightInPoints) {
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 设置边框
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.NONE);


        // 配置字体
        WriteFont contentWriteFont = new WriteFont();
        // 字体
        contentWriteFont.setFontName("宋体");
        // 字体大小
        contentWriteFont.setFontHeightInPoints(fontHeightInPoints);
        // 设置加粗
        contentWriteFont.setBold(false);
        contentWriteCellStyle.setWriteFont(contentWriteFont);

        // 【水平居中需要使用以下两行】
        // 设置文字左右居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        // 设置文字上下居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 自动换行
        contentWriteCellStyle.setWrapped(true);

        // 样式策略
        return new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
    }

}
